简体   繁体   English

Laravel 4中使用AJAX / JSON的JavaScript / jQuery问题

[英]JavaScript/jQuery Issue Using AJAX/JSON in Laravel 4

I hope you can point me in the right direction with this one. 我希望您能以此为我指明正确的方向。

I have the following JavaScript: 我有以下JavaScript:

var url = '/admin/list-contacts';

var contacts;

$.ajax({
    async: false,
    type: 'GET',
    url: url,
    dataType: 'json',
    success : function(data) { contacts = data; }
});

var accountId = $('#account_id option:selected').val();

if(accountId === 'NULL') {
    $('.delegates .contacts, .delegates .delegate_status_id, .delegates .event_id, .delegates .price, .opportunities .contacts, .notes .contacts').hide();
} else {
    $('.delegates .contacts, .delegates .delegate_status_id, .delegates .event_id, .delegates .price, .opportunities .contacts, .notes .contacts').show();
}

$('#account_id').change(function() {
    select = $("<select name='contact_id' id='contact_id'>");


    for(var i in contacts) {
        if(contacts[i]['account_id'] === $(this).val()) {
            select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
        } else {
            select.html('<option value="NULL">No Contacts</option>');
        }
    }

    var accountId = $('#account_id option:selected').val();

    if(accountId === 'NULL') {
        $('.delegates .contacts, .delegates .delegate_status_id, .delegates .event_id, .delegates .price, .opportunities .contacts, .notes .contacts').hide();
        $('.delegates .contacts .controls, .opportunities .contacts .controls, .notes .contacts .controls').html('');
    } else {
        $('.delegates .contacts, .delegates .delegate_status_id, .delegates .event_id, .delegates .price, .opportunities .contacts, .notes .contacts').show();
        $('.delegates .contacts .controls, .opportunities .contacts .controls, .notes .contacts .controls').html(select);
    }

});

Basically, what it is doing is pulling through a list of contacts via JSON with this code in Laravel 4: 基本上,它正在做的是使用Laravel 4中的以下代码通过JSON提取联系人列表:

public function contacts()
{
    return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get();
}

All good and well, the contacts variable is populated with the correct data, and I've logged the results of this variable in the console throughout the process, and it contains the correct data when a change is made to the select box. 很好,好了,contacts变量中填充了正确的数据,并且在整个过程中,我已将该变量的结果记录在控制台中,当对选择框进行更改时,它包含正确的数据。 The problem is the if statement in the for loop. 问题是for循环中的if语句。

If there is only 1 account with contacts in the system, it works fine and all the contacts are pulled through. 如果系统中只有1个带有联系人的帐户,则该帐户可以正常工作,并且所有联系人都已通过。 As soon as you add another account, it fails and reverts to using the code in the else statement. 一旦添加另一个帐户,该帐户就会失败,并恢复为使用else语句中的代码。 Funny thing is, you remove the else portion of the if statement and it works flawlessly. 有趣的是,您删除了if语句的else部分,它可以完美地工作。 I need the 'No contacts' bit though, to show that there are none if the account has none. 不过,我需要“无联系人”位,以表明如果该帐户没有账户,则没有账户。

Any ideas guys? 有想法吗?

EDIT : I should mention that the population of the contacts drop down is dependant on a previous drop down that contains the accounts in the system. 编辑 :我应该提到,联系人下拉列表的数量取决于包含系统中帐户的先前下拉列表。 The JavaScript checks for the id of the account selected and pulls through the correct contacts. JavaScript会检查所选帐户的ID,并检索正确的联系人。

The issue is with the way the loop works when you have multiple values. 问题在于当您有多个值时循环的工作方式。 If all the contacts match the selected account, then it's OK because the if part is the only bit that is ever executed. 如果所有联系人都与所选帐户匹配,则可以,因为if部分是唯一执行过的位。 However, if the else part is ever executed you're removing anything added for the successful matches. 但是,如果else部分曾经执行过,则将删除为成功匹配添加的所有内容。

Instead of using an if else structure, I'd do the following: 除了使用if else结构,我将执行以下操作:

for (var i in contacts) {
    if (contacts[i]['account_id'] === this.value) {
        select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
    }
}

if(select.find('option').length === 0) {
    select.html('<option value="NULL">No Contacts</option>');
}

That looks through all of your contacts, finding the ones that match and adding them to your <select> element. 这会遍历您所有的联系人,找到匹配的联系人并将其添加到您的<select>元素中。 Then, once it's examined all of the contacts, it checks to see if any were added to the <select> element - if none were, it adds a single "No Contacts" option to it. 然后,一旦检查了所有联系人,就会检查是否将任何内容添加到<select>元素中-如果没有添加任何元素,则会向其中添加一个“无联系人”选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM